package study.cdc.canal.support.canal.client;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;

import java.util.concurrent.atomic.AtomicInteger;

@Slf4j
public class SpringCanalClient extends CanalClient implements InitializingBean, DisposableBean {

    private static AtomicInteger NO = new AtomicInteger(1);

    public SpringCanalClient(CanalProperties props) {
        super(props);
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        //使用线程异步启动，否则会阻塞于此
        Thread t = new Thread(() -> {
            super.connect();
        }, "client-thread-" + NO.getAndIncrement());
        t.start();
    }

    @Override
    public void destroy() throws Exception {
        super.disconnect();
    }
}
